<?php 
/**
**2015-08-24 对接驴妈妈系统
**User By  PZP
**/
class LvMaMa{
	// private $appKey = 'PIAOFUTONG';
	// private $secret = 'a950a35d6b376c0c04eea56624a17ef8';
	// private $lmm_URL = 'http://180.169.51.94/api1/distributorApi/2.0/api/';  
	private $appKey = 'PIAOFUTONG';
	private $secret = 'e9c0c9f27762474d76a025238a158065';
	private $lmm_URL = 'http://api.lvmama.com/distributorApi/2.0/api/'; 
	
	public function share_array(){ 
		$time = time();
		$request_array = array(
			'timestamp' 	=> $time,
			'appKey' 		=> $this->appKey,
			'messageFormat' => 'xml',			
			'sign'			=> md5($this->secret.$time.$this->secret)
		);
		return $request_array;
	}
	
	//批量景区基本信息接口
	public function scenicInfoListByPage(){	
	
		$action  = 'ticketProd/scenicInfoListByPage';
		
		$request_array = $this->share_array();
		$request_array['currentPage'] = 1;		
		 
		$request_lmm  = $this->lmm_post($action,$request_array);
		return $request_lmm;
		
	}
	
	//批量产品/商品信息接口
	public function productInfoListByPage(){
		
		$action  = 'ticketProd/productInfoListByPage';
		
		$request_array = $this->share_array();		
		$request_array['currentPage'] = 6;
		
		$request_lmm  = $this->lmm_post($action,$request_array);
		return $request_lmm;
	}
	
	
	//批量价格/库存信息接口
	public function productPriceListByPage(){	
	
		$action  = 'ticketProd/productPriceListByPage';
		
		$request_array = $this->share_array();
		$request_array['currentPage']	= 2;
		$request_array['beginDate']		= '2015-08-25';
		$request_array['endDate'] 		= '2015-08-26';
		
		$request_lmm  = $this->lmm_post($action,$request_array);
		return $request_lmm;
	}
	
	//按ID获取产品信息接口
	public function productInfoList(){
		$action  = 'ticketProd/productInfoList';

		$request_array = $this->share_array();
		$request_array['productIds'] = '182579';
		
		$request_lmm  = $this->lmm_post($action,$request_array);
		return $request_lmm;
	}
	
	//按ID获取商品信息接口
	public function goodInfoList(){
		$action  = 'ticketProd/goodInfoList';
	
		$request_array = $this->share_array();
		$request_array['goodsIds'] = '948596';
		
		$request_lmm  = $this->lmm_post($action,$request_array);
		return $request_lmm;
	}
	
	//按ID获取价格库存接口
	public function goodPriceList(){
		$action  = 'ticketProd/goodPriceList';

		$request_array = $this->share_array();
		$request_array['goodsIds']	= '1560823';
		$request_array['beginDate']	= '2015-08-25';
		$request_array['endDate'] 	= '2015-10-26';
		
		$request_lmm  = $this->lmm_post($action,$request_array);
		return $request_lmm;
	}
	
	
	//订单校验接口	
	public function validateOrder($submit_array){
		$action  = 'ticket/validateOrder';
		
		$request_array  = $this->share_array();
		$orderInfo=<<<xml
<request>
	<orderInfo>
		<partnerOrderNo>{$submit_array['partnerOrderNo']}</partnerOrderNo> 
		<orderAmount>{$submit_array['orderAmount']}</orderAmount>
		<product>
			<productId>{$submit_array['productId']}</productId>
			<goodsId>{$submit_array['goodsId']}</goodsId>
			<quantity>{$submit_array['quantity']}</quantity>
			<visitDate>{$submit_array['visitDate']}</visitDate> 
			<sellPrice>{$submit_array['sellPrice']}</sellPrice>
		</product> 
		<booker> 
			<name>{$submit_array['name']}</name>
			<mobile>{$submit_array['mobile']}</mobile>
			<email></email>
		</booker> 
		<travellers> 
			{$submit_array['travellers']}
		</travellers> 
	</orderInfo> 
</request>
xml;
		// print_r( $orderInfo);
		$request_array['request']	= $orderInfo;
		$request_lmm_xml  = $this->lmm_post($action,$request_array);
	    if((int)$request_lmm_xml->state->code == 1000){
			$createOrder = $this->createOrder($orderInfo);
			return $createOrder;
		}else{
			$this->logfile($submit_array['partnerOrderNo'].'|'.(int)$request_lmm_xml->state->code.'|'.(string)$request_lmm_xml->state->message,'validateOrder_fail');
			exit("101|0|0|0|0|0");
		}
	}

	//创建订单
	public function createOrder($orderInfo){		
		$action  = 'ticket/createOrder';		
		$request_array  = $this->share_array();
		$request_array['request']	= $orderInfo;
		$request_lmm_xml  = $this->lmm_post($action,$request_array);
		return $request_lmm_xml;
	}
	
	//支付订单
	public function orderPayment($partnerOrderNo,$orderId){
		$action  = 'order/orderPayment';
		$serialNum = time();
		$request_array  = $this->share_array();
		$request =<<<xml
			<request>
				<order>
					<partnerOrderNo>{$partnerOrderNo}</partnerOrderNo>
					<orderId>{$orderId}</orderId>
					<serialNum>{$serialNum}</serialNum>
				</order> 
			</request>
xml;
		$request_array['request']	= $request;
		$request_lmm  = $this->lmm_post($action,$request_array);
		if((int)$request_lmm->state->code == 1000){
			$this->logfile($partnerOrderNo.'|'.$orderId,'orderPayment_success');
		}else{
			$this->logfile($partnerOrderNo.'|'.$orderId.'|'.(int)$request_lmm->state->code.'|'.(string)$request_lmm->state->message,'orderPayment_fail');
		}
	}
	
	//查询订单
	public function getOrderInfo(){
		$action  = 'order/getOrderInfo';
		$request_array  = $this->share_array();
		$request = <<<xml
		<request>
			<order>
				<partnerOrderNos>pft144607978094286073</partnerOrderNos> 
			</order>
		</request>
xml;
		$request_array['request']	= $request;
		$request_lmm  = $this->lmm_post($action,$request_array);
		print_r($request_lmm);
	}
	
	//重发凭证
	public function resendCode(){
		$action  = 'order/resendCode';
		$request_array  = $this->share_array();
		$request = <<<xml
		<request>
			<order>
				<partnerOrderNo>pft144607978094286073</partnerOrderNo>
				<orderId>25290077</orderId>
			</order>
		</request>
xml;
		$request_array['request']	= $request;
		$request_lmm  = $this->lmm_post($action,$request_array);
		print_r($request_lmm);
	}
	
	//修改取消订单
	public function orderCancel($partnerOrderNo,$orderId){
		$action  = 'ticket/orderCancel';
		$request_array  = $this->share_array();
		$request_array['PartnerOrderNo'] = $partnerOrderNo;
		$request_array['orderId'] =  $orderId;
		$request_lmm  = $this->lmm_post($action,$request_array);
		if((int)$request_lmm->state->code == 1000){
			if((string)$request_lmm->order->requestStatus == 'REVIEWING'){
				$this->logfile($partnerOrderNo.'|'.$orderId,'orderCanceling_success');
				return 100;
			}elseif((string)$request_lmm->order->requestStatus == 'REJECT'){ 
				$this->logfile($partnerOrderNo.'|'.$orderId,'orderCancel_fail');
				return 101;
			}elseif((string)$request_lmm->order->requestStatus == 'PASS'){ 
				$this->logfile($partnerOrderNo.'|'.$orderId,'lvmamam_cancel_order');
				return 102;
			}
		}else{
			$this->logfile($partnerOrderNo.'|'.$orderId.'|'.(int)$request_lmm->state->code.'|'.(string)$request_lmm->state->message,'orderCanceling_fail');
			return 101;
		}
	}
	
	
	//提交
	public function lmm_post($action,$request_array){
		$request_data  = http_build_query($request_array);
		$url = $this->lmm_URL.$action;
		$ch = curl_init(); 
		$this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
		curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);  
		curl_setopt($ch, CURLOPT_URL,$url); 	
		curl_setopt($ch, CURLOPT_POSTFIELDS, mb_convert_encoding($request_data,"GBK","UTF-8")); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$result = curl_exec($ch);
		$result = simplexml_load_string( $result, 'SimpleXMLElement', LIBXML_NOCDATA );
		return $result;
	}
	
	public function logfile($txt,$logname){
		$fp = fopen('/var/www/html/new/d/logs/api/LvMaMa/'.$logname.date('Y-m-d').'.txt',"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
	} 
}

$LMM = new LvMaMa();
// $scenicInfoListByPage = $LMM->scenicInfoListByPage();print_r($scenicInfoListByPage);
// $productInfoListByPage = $LMM->productInfoListByPage();print_r($productInfoListByPage);
// $productPriceListByPage = $LMM->productPriceListByPage();print_r($productPriceListByPage);
// $productInfoList = $LMM->productInfoList();print_r($productInfoList);
// $goodInfoList = $LMM->goodInfoList();print_r($goodInfoList);
// $goodPriceList = $LMM->goodPriceList();print_r($goodPriceList);
// $validateOrder = $LMM->validateOrder($array);//print_r($validateOrder);
// $orderPayment = $LMM->orderPayment(1,2);//print_r($validateOrder);
// $orderCancel = $LMM->orderCancel();//print_r($validateOrder);
$getOrderInfo = $LMM->getOrderInfo();//print_r($validateOrder);
// $resendCode = $LMM->resendCode();//print_r($validateOrder);
?>